package com.example.mgsnew.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

/**
 * WebSocket配置类 - 使用STOMP协议
 */
@Configuration
@EnableWebSocketMessageBroker // 启用WebSocket消息代理功能
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws/notification")  // WebSocket连接端点
                .setAllowedOrigins("*")  // 允许跨域
                .withSockJS();  // 支持SockJS(用于浏览器兼容性)
    }

//    @Override
//    public void configureMessageBroker(MessageBrokerRegistry registry) {
//        // 启用内存消息代理，客户端订阅前缀为/topic的消息
//        registry.enableSimpleBroker("/topic");
//
//        // 客户端发送消息到服务端的前缀
//        registry.setApplicationDestinationPrefixes("/app");
//    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // 设置消息缓冲区大小
        container.setMaxTextMessageBufferSize(8192);
        container.setMaxBinaryMessageBufferSize(8192);
        return container;
    }
}